#include"doctor.h"
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
bool isName(char a[])
{
    int i;
    if(strlen(a)>20)
    {
     printf("姓名长度非法\n");
      return 0;
    }
        for(i=0;i<strlen(a);i=i+2)
            if(!(a[i]&0x80&&a[i+1]&0x80))
        {
            printf("输入姓名中含有非中文字符！\n");
            return 0;
        }
        return 1;
}
DoctorNode* createNullDoctorNode()
{
    DoctorNode* temp = (DoctorNode*)malloc(sizeof(DoctorNode));
    temp->pre = temp->next = NULL;
    return temp;
}


void createWorknumber(DoctorNode *List)
{
    DoctorNode *temp=List;
    while(temp)
    {
        temp->data.worknumber[0]=temp->data.subject;
        temp->data.worknumber[1]=temp->data.level;
        temp->data.condition=1;
        for(int i=2;i<20;i++)
          temp->data.worknumber[i]=temp->data.ID_number[i-2];
          temp=temp->next;

    }
}


exDoctorNode*createNullexDoctorNode()
{
    exDoctorNode* temp = (exDoctorNode*)malloc(sizeof(exDoctorNode));
    temp->pre = temp->next = NULL;
    return temp;
}

void judge_condition(FILE *fp,DoctorNode *List)
{
    int temp1;
    exDoctorNode* temp = createNullexDoctorNode();
    if(5 != fscanf(fp,"%d %d  %d %d %s",
        &(temp->time.year),&(temp->time.month),
        &(temp->time.day),&(temp->extra_condition),temp->data1.ID_number))
    {
        printf("ERROR:Fail to read\n");
        return;
    }
    Date nowtime=getNowtime();
    if(temp->extra_condition==0)
        {
            delete_Doctor(List,temp->data1.ID_number);
            return;
        }
    else temp1=2;
    if(nowtime.year==temp->time.year&&nowtime.month==temp->time.month&&nowtime.day==temp->time.day)
        polish_Doctor(List,temp->data1.ID_number,temp1);
}

void polish_Doctor(DoctorNode *List,char ID_number[18],int condition)
{
    DoctorNode* temp=List;
    while(temp&&strcmp(temp->data.ID_number,ID_number)!=0)
    {
        temp=temp->next;
    }
    if(temp)
    {
        temp->data.condition=condition;
    }

}
DoctorNode* find_Doctor(DoctorNode*List,char a[18])

{
    DoctorNode* temp = List;
    // if(isListEmpty(List))
    // {
    //     printf("ERROR:List hasn't been created！\n");
    //     return 0;
    // }
    while(temp && strcmp(temp->data.ID_number,a)!=0)
        temp = temp->next;
    if(temp)
        return temp;
    else
    {
        printf("ERROR:There is no doctor with that ID_number in this List\n");
        return 0;
    }
}

bool delete_Doctor(DoctorNode*List,char a[18])
{

    DoctorNode* temp = find_Doctor(List,a);
    if(temp)
    {
        if(temp->next)
        {
            temp->pre->next = temp->next;
            temp->next->pre = temp->pre;
        }
        else
        {
            temp->pre->next = NULL;
        }
        free(temp);
        return 1;
    }
    else
    {
        printf("ERROR:No doctor like this OR No List!");
        return 0;
    }

}

DoctorNode* createNullDoctorNode()
{
    DoctorNode *temp = (DoctorNode*)malloc(sizeof(DoctorNode));
    temp->pre = temp->next = NULL;
    return temp;
}

DoctorNode* readADoctor(FILE* fp)
{
    if(!fp)
    {
        printf("ERROR:Fail to read file!\n");
        return 0;
    }

    DoctorNode* temp = createNullDoctorNode();
    if(5 != fscanf(fp,"%s %s  %c %c %s",
        temp->data.name,temp->data.ID_number,
        &(temp->data.level),&(temp->data.subject),
        temp->data.worknumber))
    {
        printf("ERROR:Fail to read\n");
        return 0;
    }
    for(int i = 0;i < 7;i++)
        fscanf(fp,"%d",&temp->data.ispresent[i]);
    fscanf(fp,"%d",&temp->data.condition);
    for(int i = 0;i < 12;i++)
        for(int j = 0;j < 31;j++)
            fscanf(fp,"%d",&temp->data.day_worktimes[i][j]);

    //�˴���׳�Ի�����
    return temp;
}

DoctorNode* loadDoctorList()
{
    FILE* fp = NULL;
    if((fp = fopen("doctor.json","r")) == 0)
    {
        printf("ERROR:Fail to open doctor.json");
        return 0;
    }
    fscanf(fp,"%d",&DoctorNum);
    DoctorNode *DoctorList = readADoctor(fp);
    DoctorNode *temp = DoctorList;
    int num = 1;
    while(!feof(fp) && num++ < DoctorNum)
    {
        temp->next = readADoctor(fp);
        if(temp->next == NULL)
        {
            printf("ERROR: Can't read this Doctor\n");
            return 0;
        }
        temp->next->pre = temp;
        temp = temp->next;
    }
    fclose(fp);
    return DoctorList;
}

bool writeADoctor(FILE *fp,DoctorNode *doctor)
{
    if(5 != fprintf(fp,"%s %s %c %c %s",
        doctor->data.name,doctor->data.ID_number,
        doctor->data.level,doctor->data.subject,
        doctor->data.worknumber))
    {
        printf("ERROR:Fail to Write\n");
        return 0;
    }
    else
    {
        return 1;
    }

}

bool writeDoctorList(DoctorNode *DoctorList)
{
    FILE *fp = NULL;
    if(!(fp = fopen("doctor.json","W")))
    {
        printf("ERROR:Fail to open doctor.json\n");
        return 0;
    }

    DoctorNode* temp = DoctorList;
    fprintf(fp,"%d ",DoctorNum);
    int num = 0;
    while(temp && num++ < DoctorNum)
    {
        if(!writeADoctor(fp,temp))
        {
            printf("ERROR:Fail to write Doctor NO.%d\n",num + 1);
            return 0;
        }
        temp = temp->next;
    }
    fclose(fp);
    return 1;
}
